//
//  MainTarBarController.swift
//  XDweibo
//
//  Created by HanXingda on 15/6/23.
//  Copyright © 2015年 韩兴达. All rights reserved.
//

import UIKit

class MainTarBarController: UITabBarController {

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        tabBar.addSubview(compoesButton)

    }
    
    func compoesButtonClick() {
        let nav = UINavigationController(rootViewController: ComposeViewController())
        
        presentViewController(nav, animated: true, completion: nil)
    }

    private func addChildViewControllers() {

                    addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
                    addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
                    addChildViewController(UIViewController(), title: "", imageName: "")
                    addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
                    addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")

    }
    private func addChildViewController(vc: UIViewController, title : String, imageName : String) {
        
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        vc.title = title
        
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    
    ///懒加载
    lazy var compoesButton:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)

        let w = self.tabBar.bounds.width / CGFloat(5)
        let h = self.tabBar.bounds.height
        let rect = CGRectMake(0, 0, w, h)
        btn.frame = CGRectOffset(rect, CGFloat(2) * w, 0)
        
        
        
        btn.addTarget(self, action: "compoesButtonClick", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
        }()

}
